//
//  TTAddFeebackViewController.m
//  Estate
//
//  Created by houzhen on 2018/1/5.
//  Copyright © 2018年 ttsx. All rights reserved.
//

#import "TTAddFeebackViewController.h"
#import "QianDTableCell.h"
#import "B_wuLiaoCell.h"

#import "TPKeyboardAvoidingTableView.h"
@interface TTAddFeebackViewController ()<UITableViewDelegate,UITableViewDataSource>
/**注释 */
@property (strong ,nonatomic)TPKeyboardAvoidingTableView *tableView;
@property (nonatomic,copy) NSString * titleName;
@property (nonatomic,copy) NSString * content;

@end

@implementation TTAddFeebackViewController
-(UITableView *)tableView{
    
    if(_tableView==nil){
        _tableView=[[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRectMake(0, 64, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64)];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"QianDTableCell" bundle:nil] forCellReuseIdentifier:@"qiandCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"B_wuLiaoCell" bundle:nil] forCellReuseIdentifier:@"wuliaoCell"];
        
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"问题反馈";
    self.titleName = self.model.opinionTitle;
    self.content = self.model.opinionContent;
    [self.view addSubview:self.tableView];
    
    self.tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
    self.view.backgroundColor=[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    self.tableView.backgroundColor=[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    // Do any additional setup after loading the view.
}
-(void)commit{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (self.titleName.length==0) {
        [LSUtility showToastWithMessage:@"请输入标题" inView:self.view Andtime:1.5];
        return;
    }
    if (self.content.length==0) {
        [LSUtility showToastWithMessage:@"请输入内容" inView:self.view Andtime:1.5];
        return;
    }

    // 获取项目ID
    NSString *proID = DEF_PERSISTENT_GET_OBJECT(@"proid");
    // 获取用户ID
    NSString *userID = DEF_PERSISTENT_GET_OBJECT(@"userid");
    [parameters setValue:@"" forKey:@"id"];
    [parameters setValue:proID forKey:@"proId"];
    [parameters setValue:userID forKey:@"opinionUserId"];
    NSString *opinionUserType =  DEF_PERSISTENT_GET_OBJECT(@"role");
    [parameters setValue:opinionUserType forKey:@"opinionUserType"];
    [parameters setValue:@(1) forKey:@"opinionFeedbackState"];
    [parameters setValue:self.titleName forKey:@"opinionTitle"];
    [parameters setValue:self.content forKey:@"opinionContent"];
    [parameters setValue:@(1) forKey:@"opinionProcessStage"];
    WEAKSELF
    [LSHttpTool Post:URL_insertOrReplace parameters:parameters success:^(id responseObject) {
        if ([[responseObject objectForKey:@"code"]isEqualToString:@"1"]) {
            
            weakSelf.block();
            [self.navigationController popViewControllerAnimated:YES];
            
        } else {
       
        }
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF

    if(indexPath.row == 0){
        QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qiandCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.title.text = @"标题:";
        cell.isedit = self.isEdit;
        cell.textField.placeholder = @"请输入反馈问题名称";
        cell.textField.text = self.titleName;
        cell.textValueChangedBlock = ^(NSString *valueStr) {
            weakSelf.titleName = valueStr;
        };
        return cell;
        
    }else {
        B_wuLiaoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"wuliaoCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        //能否编辑
        cell.isEdit = self.isEdit;
        cell.title.text = @"反馈内容:";
        cell.text.placeholder = @"请输入反馈问题内容";
        cell.text.text = self.content;
        cell.textValueChangedBlock = ^(NSString *valueStr){
            weakSelf.content = valueStr;
        };
        return cell;
        
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        return 44;
    }else{
        return 200;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if(!self.isEdit){
        return 0;
    }else{
        return 50;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if(!self.isEdit){
        return nil;
    }else{
        UIView *fotterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 200)];
        UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(DEF_SCREEN_WIDTH-60, 10, 50, 30)];
        [addBtn setTitle:@"提交" forState:UIControlStateNormal];
        addBtn.layer.cornerRadius = 5;
        addBtn.layer.masksToBounds = YES;
        [addBtn setBackgroundColor:[TTTheme defaultManger].defaultColor];
        addBtn.titleLabel.textColor = [UIColor blackColor];
        [addBtn addTarget:self action:@selector(commit) forControlEvents:UIControlEventTouchUpInside];
        [fotterView addSubview:addBtn];
        return fotterView;
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
